﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Truron.Purchase.Interface;
using Truron.Purchase.Api.Utility;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Truron.Purchase.Model;
using Newtonsoft.Json;
using Truron.Utility;
using Newtonsoft.Json.Linq;
using Truron.SysCenter.Interface.Remote;

namespace Truron.Purchase.Api.Controllers
{
     
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Purchase_OtherDataController : ControllerBase
    {
        private IDatabase db;
        private IRedis redis;
        private readonly Purchase_OtherDataService _Purchase_OtherDataService;
        private readonly ISysCenterRemoteService _SysCenterRemoteService;
        public Purchase_OtherDataController(IRedis Redis,IDatabase database,Purchase_OtherDataService Purchase_OtherDataService, ISysCenterRemoteService SysCenterRemoteService)
        {
            db = database;
            redis = Redis;
            Purchase_OtherDataService = Purchase_OtherDataService;
            _SysCenterRemoteService = SysCenterRemoteService;
        }
        [HttpPost]
        public IActionResult GetList(LayPageModel param)
        {
            var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
            int pageSize = Convert.ToInt32(param.limit), pageIndex = Convert.ToInt32(param.page);
            var Tuple =  _Purchase_OtherDataService.GetList(param.searchKey,pageSize, pageIndex,param.searchJson);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = Tuple.list, count = Tuple.count }));
        }
        [HttpPost]
        public IActionResult Save(RecParam param)
        {
            try
            {
                Purchase_OtherData entity = JsonConvert.DeserializeObject<Purchase_OtherData>(param.strEntity);
                var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
                if(entity.Id==0)
                {
                    entity.Id = entity.Create(userToken);
                    db.Insert(entity);
                }
                else
                {
                    entity.Modify(userToken);
                    db.Update(entity);
                }
                db.Commit();
                redis.UpdateRedisCache(entity);
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = entity, code=0 }));
            }
            catch(Exception ex)
            {
                db.Rollback();
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { code = -1, data = ex.ToString() }));
            }
            
           
        }
        public IActionResult GetDetail(Purchase_OtherData param)
        {
            if (param.Id <= 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            var ent = _Purchase_OtherDataService.GetDetail(param.Id);
            if (ent == null)
                return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = null }));
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = ent,code=0 }));
        }
        public class RecParam
        {
            public string strEntity { get; set; }
        }
        [HttpPost]
        public IActionResult Del(RecParam param)
        {
            if (param.strEntity.Length == 0)
                return Ok(JsonConvert.SerializeObject(new ResultData(ErrorCodeConst.ParameterError)));
            List<int> ids = JsonConvert.DeserializeObject<List<int>>(param.strEntity);
            var userToken = _SysCenterRemoteService.GetUserToken(HttpContext);
            var resultCode =  _Purchase_OtherDataService.Del(ids, userToken);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>() { data = resultCode, code=0 }));
        }
        
    }
}